/*
 * PlayerManagerPanel.java
 *
 * Created on October 9, 2008, 11:26 AM
 */
package com.bff.slimserver.musicjukebox.ui;

import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.slimserver.musicjukebox.lib.events.PlayerChosenEvent;
import com.bff.slimserver.musicjukebox.lib.events.PlayerChosenListener;
import com.bff.bjj.ui.component.BJJPanel;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bff.slimserver.SlimPlayer;
import org.bff.slimserver.events.PlayerChangeEvent;
import org.bff.slimserver.events.PlayerChangeListener;
import org.bff.slimserver.exception.SlimConnectionException;

/**
 *
 * @author  bfindeisen
 */
public class PlayerManagerPanel
        extends BJJPanel
        implements PlayerChangeListener, PlayerChosenListener, ComponentListener {

    private JukeboxProperties jp;
    private HashMap<SlimPlayer, PlayerManagerDetailPanel> panelMap;
    private int rows;
    private int columns;
    private boolean usingGrid;

    /** Creates new form PlayerManagerPanel */
    public PlayerManagerPanel() {
        jp = JukeboxProperties.getInstance();

        initComponents();
        playerPanel.setPreferredSize(new Dimension(1024, 768));

        panelMap = new HashMap<SlimPlayer, PlayerManagerDetailPanel>();
        loadPlayers();

        setPlayerPanelSize();

        jp.getSlimMonitor().addPlayerChangeListener(this);
        jp.addPlayerChosenListener(this);
        addComponentListener(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        playerPanel = new BJJPanel();

        setLayout(new java.awt.BorderLayout());

        playerPanel.setLayout(new java.awt.GridLayout(1, 3));
        jScrollPane1.setViewportView(playerPanel);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel playerPanel;
    // End of variables declaration//GEN-END:variables

    @Override
    public void playerChanged(PlayerChangeEvent event) {
        switch (event.getId()) {
            case PlayerChangeEvent.PLAYER_ADDED:
            case PlayerChangeEvent.PLAYER_DELETED:
                loadPlayers();
                setPlayerPanelSize();
                break;
            case PlayerChangeEvent.PLAYER_UNSYNCED:
            case PlayerChangeEvent.PLAYER_SYNCED:
                Set<SlimPlayer> s = panelMap.keySet();
                for (SlimPlayer player : s) {
                    panelMap.get(player).checkSync();
                }
                break;
        }
    }

    private void loadPlayers() {
        playerPanel.removeAll();

        panelMap = new HashMap<SlimPlayer, PlayerManagerDetailPanel>();

        try {
            List<SlimPlayer> playerList = jp.getAllPlayers();

            int size = playerList.size();

            if (size < 3) {
                usingGrid = false;
                rows = 3;
                columns = size;
                rows = 1;
                playerPanel.setLayout(new FlowLayout());
            } else {
                usingGrid = true;
                rows = size / 3;
                if (size % 3 > 0) {
                    ++rows;
                }
                columns = 3;
                playerPanel.setLayout(new GridLayout(rows, columns));
            }

            for (SlimPlayer player : playerList) {
                panelMap.put(player, new PlayerManagerDetailPanel(player));
                playerPanel.add(panelMap.get(player));
            }

            panelMap.get(jp.getPlayer()).setCurrent(true);

        } catch (SlimConnectionException ex) {
            Logger.getLogger(PlayerManagerPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void playerChosen(PlayerChosenEvent event) {
        loadPlayers();
    }

    @Override
    public void componentResized(ComponentEvent e) {
        if (usingGrid) {
            setPlayerPanelSize();
        }
    }

    @Override
    public void componentMoved(ComponentEvent e) {
    }

    @Override
    public void componentShown(ComponentEvent e) {
    }

    @Override
    public void componentHidden(ComponentEvent e) {
    }

    private void setPlayerPanelSize() {
        int size = panelMap.size();
        int width = 0;
        int height = 0;
        if (size > 0) {
            Set<SlimPlayer> s = panelMap.keySet();
            for (SlimPlayer player : s) {
                width += panelMap.get(player).getPreferredSize().getWidth() + 20;
                height += panelMap.get(player).getPreferredSize().getHeight() + 20;
                break;
            }
        }
        playerPanel.setPreferredSize(new Dimension(width, height * rows));
    }
}
